package top.yehot.kettle.boot.core.config;

import java.util.List;
import java.util.Optional;

import org.quartz.Scheduler;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import lombok.AllArgsConstructor;
import top.yehot.kettle.boot.core.task.KettleJobListener;
import top.yehot.kettle.boot.enums.GlobalParamEnum;
import top.yehot.kettle.boot.models.entity.GlobalParam;
import top.yehot.kettle.boot.service.GlobalParamService;

/**
 * 系统启动完成后初始化系统
 * 
 * @author lquan
 * @date 2022年6月6日 上午2:25:01
 */
@Component
@AllArgsConstructor
public class StartCompleteCfg implements ApplicationRunner {

	private final Scheduler scheduler;
	private final GlobalParamService globalParamService;

	@Override
	public void run(ApplicationArguments args) throws Exception {
		// 绑定监听
		scheduler.getListenerManager().addJobListener(new KettleJobListener());
		
		// 查询环境变量
		List<GlobalParam> gpList = globalParamService.list(Wrappers.lambdaQuery(GlobalParam.class).eq(GlobalParam::getCmmv, GlobalParamEnum.ENV.getValue()));
		Optional.ofNullable(gpList).ifPresent(pls -> {
			pls.stream().forEach(p -> System.getProperties().put(p.getPkey(), p.getPvalue()));
		});
	}

}
